﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace SpriteAnimator
{
    /// <summary>
    /// Fenêtre de saisie d'une texture
    /// </summary>
    public partial class frmTexture: Form
    {
        #region Constructeur

        /// <summary>
        /// Constructeur de la form
        /// </summary>
        public frmTexture(Image p_oLast)
        {
            this.InitializeComponent();
            this.DialogResult = DialogResult.Cancel;

            // *** Chargement de l'image précédente ***

            if ( p_oLast != null )
                this.picPreview.Image = (Image)p_oLast.Clone();
        }

        #endregion

        #region Evénements de l'IHM

        /// <summary>
        /// Charge une texture
        /// </summary>
        /// <param name="sender">Source de l'appel</param>
        /// <param name="e">Argument de l'appel</param>
        private void cmdBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                if ( this.dlgOpen.ShowDialog(this) == DialogResult.OK )
                {
                    if ( this.picPreview.Image != null )
                    {
                        this.picPreview.Image.Dispose();
                        this.picPreview.Image = null;
                    }

                    this.picPreview.Image = Image.FromFile(this.dlgOpen.FileName);
                }
            }
            catch ( Exception ex )
            {
                MessageBox.Show(this, string.Format("Erreur lors du chargement de l'image.\n\n{0}", ex.Message), "Texture", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Valide la texture
        /// </summary>
        /// <param name="sender">Source de l'appel</param>
        /// <param name="e">Argument de l'appel</param>
        private void cmdOk_Click(object sender, EventArgs e)
        {
            if ( this.picPreview.Image == null )
            {
                MessageBox.Show(this, "Vous devez sélectionner une texture.", "Texture", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// Annule la saisie
        /// </summary>
        /// <param name="sender">Source de l'appel</param>
        /// <param name="e">Argument de l'appel</param>
        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// Affiche la texture
        /// </summary>
        /// <param name="sender">Source de l'appel</param>
        /// <param name="e">Argument de l'appel</param>
        private void picPreview_Paint(object sender, PaintEventArgs e)
        {
            // *** On efface tout ***

            e.Graphics.Clear(this.picPreview.BackColor);

            // *** Rescale ? ***

            if ( this.picPreview.Image != null )
            {
                Rectangle oFinal;

                if ( this.picPreview.Image.Width > this.picPreview.Width || this.picPreview.Image.Height > this.picPreview.Height )
                {
                    if ( this.picPreview.Image.Width > this.picPreview.Image.Height )
                    {
                        oFinal = new Rectangle(0, 0, this.picPreview.Width, Convert.ToInt32(Convert.ToSingle(this.picPreview.Width) * (Convert.ToSingle(this.picPreview.Image.Height) / Convert.ToSingle(this.picPreview.Image.Width))));
                        oFinal.Y = (this.picPreview.Height - oFinal.Height) / 2;
                    }
                    else
                    {
                        oFinal = new Rectangle(0, 0, Convert.ToInt32(Convert.ToSingle(this.picPreview.Height) * (Convert.ToSingle(this.picPreview.Image.Width) / Convert.ToSingle(this.picPreview.Image.Height))), this.picPreview.Height);
                        oFinal.X = (this.picPreview.Width - oFinal.Width) / 2;
                    }
                }
                else
                    oFinal = new Rectangle((this.picPreview.Width / 2) - (this.picPreview.Image.Width / 2), (this.picPreview.Height / 2) - (this.picPreview.Image.Height / 2), this.picPreview.Image.Width, this.picPreview.Image.Height);

                // *** On dessine ***

                e.Graphics.DrawImage(this.picPreview.Image, oFinal, new Rectangle(0, 0, this.picPreview.Image.Width, this.picPreview.Image.Height), GraphicsUnit.Pixel);
            }
        }

        #endregion

        #region Indexer

        /// <summary>
        /// Retourne l'image en sélection
        /// </summary>
        public Image Texture
        {
            get
            {
                return (this.picPreview.Image);
            }
        }

        #endregion
    }
}
